<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>全平台舆情统计 - 舆情分析系统</title>
    <link rel="stylesheet" href="../../lib/layui/css/layui.css">
    <link rel="stylesheet" href="../../css/common.css">
    <style>
        .container {
            padding: 20px;
            background: #f5f5f5;
            min-height: 100vh;
        }
        
        .page-header {
            background: white;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .page-title {
            font-size: 24px;
            font-weight: bold;
            color: #333;
            margin: 0;
        }
        
        .filter-section {
            background: white;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .chart-container {
            background: white;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .chart-title {
            font-size: 18px;
            font-weight: bold;
            color: #333;
            margin-bottom: 20px;
        }
        
        .chart-wrapper {
            height: 400px;
        }
        
        .stats-cards {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }
        
        .stat-card {
            background: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            text-align: center;
        }
        
        .stat-number {
            font-size: 28px;
            font-weight: bold;
            color: #1890ff;
            margin-bottom: 10px;
        }
        
        .stat-label {
            color: #666;
            font-size: 14px;
        }
        
        .platform-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }
        
        .platform-card {
            background: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .platform-header {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
        }
        
        .platform-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            margin-right: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
        }
        
        .platform-name {
            font-size: 16px;
            font-weight: bold;
            color: #333;
        }
        
        .platform-stats {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 10px;
        }
        
        .platform-stat {
            text-align: center;
        }
        
        .platform-stat-number {
            font-size: 20px;
            font-weight: bold;
            color: #1890ff;
        }
        
        .platform-stat-label {
            font-size: 12px;
            color: #666;
        }
        
        .loading {
            text-align: center;
            padding: 50px;
            color: #999;
        }
    </style>
</head>
<body>
    <div id="app" class="container">
        <!-- 页面头部 -->
        <div class="page-header">
            <h1 class="page-title">全平台舆情统计</h1>
        </div>

        <!-- 筛选条件 -->
        <div class="filter-section">
            <form class="layui-form" lay-filter="filterForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">统计日期</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" id="dateRange" placeholder="请选择日期范围">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">平台</label>
                        <div class="layui-input-inline">
                            <select name="platform" lay-filter="platform">
                                <option value="">全部平台</option>
                                <option value="weibo">微博</option>
                                <option value="zhihu">知乎</option>
                                <option value="douyin">抖音</option>
                                <option value="bilibili">B站</option>
                                <option value="toutiao">头条</option>
                                <option value="douban">豆瓣</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="button" class="layui-btn" @click="searchData">查询</button>
                        <button type="button" class="layui-btn layui-btn-primary" @click="resetFilter">重置</button>
                    </div>
                </div>
            </form>
        </div>

        <!-- 总体统计卡片 -->
        <div class="stats-cards">
            <div class="stat-card">
                <div class="stat-number">{{ stats.totalArticles }}</div>
                <div class="stat-label">总文章数</div>
            </div>
            <div class="stat-card">
                <div class="stat-number">{{ stats.totalComments }}</div>
                <div class="stat-label">总评论数</div>
            </div>
            <div class="stat-card">
                <div class="stat-number">{{ stats.avgSentiment }}</div>
                <div class="stat-label">平均情感得分</div>
            </div>
            <div class="stat-card">
                <div class="stat-number">{{ stats.activePlatforms }}</div>
                <div class="stat-label">活跃平台数</div>
            </div>
        </div>

        <!-- 平台统计卡片 -->
        <div class="platform-grid">
            <div class="platform-card" v-for="platform in platformStats" :key="platform.platform">
                <div class="platform-header">
                    <div class="platform-icon" :style="{backgroundColor: platform.color}">
                        {{ platform.icon }}
                    </div>
                    <div class="platform-name">{{ platform.name }}</div>
                </div>
                <div class="platform-stats">
                    <div class="platform-stat">
                        <div class="platform-stat-number">{{ platform.articleCount }}</div>
                        <div class="platform-stat-label">文章数</div>
                    </div>
                    <div class="platform-stat">
                        <div class="platform-stat-number">{{ platform.commentCount }}</div>
                        <div class="platform-stat-label">评论数</div>
                    </div>
                    <div class="platform-stat">
                        <div class="platform-stat-number">{{ platform.sentimentScore }}</div>
                        <div class="platform-stat-label">情感得分</div>
                    </div>
                    <div class="platform-stat">
                        <div class="platform-stat-number">{{ platform.engagementRate }}%</div>
                        <div class="platform-stat-label">互动率</div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 平台数据对比 -->
        <div class="chart-container">
            <h3 class="chart-title">平台数据对比</h3>
            <div id="platformComparisonChart" class="chart-wrapper"></div>
        </div>

        <!-- 情感分布统计 -->
        <div class="chart-container">
            <h3 class="chart-title">全平台情感分布</h3>
            <div id="sentimentDistributionChart" class="chart-wrapper"></div>
        </div>

        <!-- 平台活跃度趋势 -->
        <div class="chart-container">
            <h3 class="chart-title">平台活跃度趋势</h3>
            <div id="activityTrendChart" class="chart-wrapper"></div>
        </div>

        <!-- 热门话题分布 -->
        <div class="chart-container">
            <h3 class="chart-title">热门话题分布</h3>
            <div id="topicDistributionChart" class="chart-wrapper"></div>
        </div>
    </div>

    <!-- 引入依赖 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script src="../../lib/echarts/echarts.min.js"></script>
    <script src="../../lib/layui/layui.js"></script>
    <script src="../../js/api.js"></script>
    
    <script>
        new Vue({
            el: '#app',
            data: {
                stats: {
                    totalArticles: 0,
                    totalComments: 0,
                    avgSentiment: 0,
                    activePlatforms: 0
                },
                platformStats: [],
                platformComparisonChart: null,
                sentimentDistributionChart: null,
                activityTrendChart: null,
                topicDistributionChart: null,
                filterParams: {
                    startDate: '',
                    endDate: '',
                    platform: ''
                },
                loading: false
            },
            mounted() {
                this.initLayui();
                this.initCharts();
                this.loadData();
            },
            methods: {
                // 初始化Layui
                initLayui() {
                    layui.use(['form', 'laydate'], () => {
                        const form = layui.form;
                        const laydate = layui.laydate;
                        
                        // 初始化日期选择器
                        laydate.render({
                            elem: '#dateRange',
                            type: 'date',
                            range: true,
                            done: (value) => {
                                if (value) {
                                    const dates = value.split(' - ');
                                    this.filterParams.startDate = dates[0];
                                    this.filterParams.endDate = dates[1];
                                }
                            }
                        });
                        
                        form.render();
                    });
                },

                // 初始化图表
                initCharts() {
                    this.platformComparisonChart = echarts.init(document.getElementById('platformComparisonChart'));
                    this.sentimentDistributionChart = echarts.init(document.getElementById('sentimentDistributionChart'));
                    this.activityTrendChart = echarts.init(document.getElementById('activityTrendChart'));
                    this.topicDistributionChart = echarts.init(document.getElementById('topicDistributionChart'));
                },

                // 加载数据
                async loadData() {
                    try {
                        this.loading = true;
                        
                        // 加载统计数据
                        await this.loadStats();
                        
                        // 加载平台数据
                        await this.loadPlatformData();
                        
                        // 加载图表数据
                        await this.loadChartData();
                        
                    } catch (error) {
                        console.error('加载数据失败:', error);
                        layui.use('layer', () => {
                            layui.layer.msg('加载数据失败', {icon: 2});
                        });
                    } finally {
                        this.loading = false;
                    }
                },

                // 加载统计数据
                async loadStats() {
                    try {
                        const response = await API.opinion.getPlatformStats(this.filterParams);
                        this.stats = response;
                    } catch (error) {
                        console.error('加载统计数据失败:', error);
                        // 使用模拟数据
                        this.stats = {
                            totalArticles: 1250,
                            totalComments: 8560,
                            avgSentiment: 0.42,
                            activePlatforms: 6
                        };
                    }
                },

                // 加载平台数据
                async loadPlatformData() {
                    try {
                        const response = await API.opinion.getPlatformDetails(this.filterParams);
                        this.platformStats = response;
                    } catch (error) {
                        console.error('加载平台数据失败:', error);
                        // 使用模拟数据
                        this.platformStats = [
                            {
                                platform: 'weibo',
                                name: '微博',
                                icon: '微',
                                color: '#e6162d',
                                articleCount: 320,
                                commentCount: 2100,
                                sentimentScore: 0.45,
                                engagementRate: 85
                            },
                            {
                                platform: 'zhihu',
                                name: '知乎',
                                icon: '知',
                                color: '#0084ff',
                                articleCount: 280,
                                commentCount: 1800,
                                sentimentScore: 0.38,
                                engagementRate: 92
                            },
                            {
                                platform: 'douyin',
                                name: '抖音',
                                icon: '抖',
                                color: '#000000',
                                articleCount: 250,
                                commentCount: 1500,
                                sentimentScore: 0.52,
                                engagementRate: 78
                            },
                            {
                                platform: 'bilibili',
                                name: 'B站',
                                icon: 'B',
                                color: '#00a1d6',
                                articleCount: 200,
                                commentCount: 1200,
                                sentimentScore: 0.41,
                                engagementRate: 88
                            },
                            {
                                platform: 'toutiao',
                                name: '头条',
                                icon: '头',
                                color: '#ff6b35',
                                articleCount: 150,
                                commentCount: 900,
                                sentimentScore: 0.35,
                                engagementRate: 75
                            },
                            {
                                platform: 'douban',
                                name: '豆瓣',
                                icon: '豆',
                                color: '#2e963d',
                                articleCount: 50,
                                commentCount: 1060,
                                sentimentScore: 0.28,
                                engagementRate: 95
                            }
                        ];
                    }
                },

                // 加载图表数据
                async loadChartData() {
                    try {
                        const response = await API.opinion.getPlatformCharts(this.filterParams);
                        this.updateCharts(response);
                    } catch (error) {
                        console.error('加载图表数据失败:', error);
                        // 使用模拟数据
                        this.updateCharts(this.getMockChartData());
                    }
                },

                // 更新图表
                updateCharts(data) {
                    this.updatePlatformComparisonChart(data);
                    this.updateSentimentDistributionChart(data);
                    this.updateActivityTrendChart(data);
                    this.updateTopicDistributionChart(data);
                },

                // 更新平台对比图表
                updatePlatformComparisonChart(data) {
                    const platforms = this.platformStats.map(p => p.name);
                    const articleData = this.platformStats.map(p => p.articleCount);
                    const commentData = this.platformStats.map(p => p.commentCount);
                    
                    const option = {
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {
                                type: 'shadow'
                            }
                        },
                        legend: {
                            data: ['文章数', '评论数']
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: {
                            type: 'category',
                            data: platforms
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [
                            {
                                name: '文章数',
                                type: 'bar',
                                data: articleData
                            },
                            {
                                name: '评论数',
                                type: 'bar',
                                data: commentData
                            }
                        ]
                    };
                    this.platformComparisonChart.setOption(option);
                },

                // 更新情感分布图表
                updateSentimentDistributionChart(data) {
                    const positive = data.sentimentDistribution?.positive || 45;
                    const neutral = data.sentimentDistribution?.neutral || 35;
                    const negative = data.sentimentDistribution?.negative || 20;
                    
                    const option = {
                        tooltip: {
                            trigger: 'item',
                            formatter: '{a} <br/>{b}: {c} ({d}%)'
                        },
                        legend: {
                            orient: 'vertical',
                            left: 'left'
                        },
                        series: [
                            {
                                name: '情感分布',
                                type: 'pie',
                                radius: ['40%', '70%'],
                                avoidLabelOverlap: false,
                                label: {
                                    show: false,
                                    position: 'center'
                                },
                                emphasis: {
                                    label: {
                                        show: true,
                                        fontSize: '18',
                                        fontWeight: 'bold'
                                    }
                                },
                                labelLine: {
                                    show: false
                                },
                                data: [
                                    { value: positive, name: '正面', itemStyle: { color: '#52c41a' } },
                                    { value: neutral, name: '中性', itemStyle: { color: '#faad14' } },
                                    { value: negative, name: '负面', itemStyle: { color: '#ff4d4f' } }
                                ]
                            }
                        ]
                    };
                    this.sentimentDistributionChart.setOption(option);
                },

                // 更新活跃度趋势图表
                updateActivityTrendChart(data) {
                    const dates = data.activityTrend?.dates || ['01-01', '01-02', '01-03', '01-04', '01-05', '01-06', '01-07'];
                    const weiboData = data.activityTrend?.weibo || [120, 132, 101, 134, 90, 230, 210];
                    const zhihuData = data.activityTrend?.zhihu || [220, 182, 191, 234, 290, 330, 310];
                    const douyinData = data.activityTrend?.douyin || [150, 232, 201, 154, 190, 330, 410];
                    
                    const option = {
                        tooltip: {
                            trigger: 'axis'
                        },
                        legend: {
                            data: ['微博', '知乎', '抖音']
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: {
                            type: 'category',
                            boundaryGap: false,
                            data: dates
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [
                            {
                                name: '微博',
                                type: 'line',
                                stack: 'Total',
                                data: weiboData
                            },
                            {
                                name: '知乎',
                                type: 'line',
                                stack: 'Total',
                                data: zhihuData
                            },
                            {
                                name: '抖音',
                                type: 'line',
                                stack: 'Total',
                                data: douyinData
                            }
                        ]
                    };
                    this.activityTrendChart.setOption(option);
                },

                // 更新话题分布图表
                updateTopicDistributionChart(data) {
                    const topics = data.topicDistribution?.topics || ['科技', '社会', '娱乐', '体育', '财经', '教育'];
                    const values = data.topicDistribution?.values || [30, 25, 20, 15, 5, 5];
                    
                    const option = {
                        tooltip: {
                            trigger: 'item'
                        },
                        series: [
                            {
                                name: '话题分布',
                                type: 'pie',
                                radius: '50%',
                                data: topics.map((topic, index) => ({
                                    value: values[index],
                                    name: topic
                                })),
                                emphasis: {
                                    itemStyle: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                }
                            }
                        ]
                    };
                    this.topicDistributionChart.setOption(option);
                },

                // 获取模拟图表数据
                getMockChartData() {
                    return {
                        sentimentDistribution: {
                            positive: 45,
                            neutral: 35,
                            negative: 20
                        },
                        activityTrend: {
                            dates: ['01-01', '01-02', '01-03', '01-04', '01-05', '01-06', '01-07'],
                            weibo: [120, 132, 101, 134, 90, 230, 210],
                            zhihu: [220, 182, 191, 234, 290, 330, 310],
                            douyin: [150, 232, 201, 154, 190, 330, 410]
                        },
                        topicDistribution: {
                            topics: ['科技', '社会', '娱乐', '体育', '财经', '教育'],
                            values: [30, 25, 20, 15, 5, 5]
                        }
                    };
                },

                // 查询数据
                searchData() {
                    this.loadData();
                },

                // 重置筛选条件
                resetFilter() {
                    this.filterParams = {
                        startDate: '',
                        endDate: '',
                        platform: ''
                    };
                    layui.use('form', () => {
                        layui.form.val('filterForm', this.filterParams);
                    });
                    this.loadData();
                }
            }
        });

        // 响应式处理
        window.addEventListener('resize', () => {
            const app = document.querySelector('#app').__vue__;
            if (app.platformComparisonChart) app.platformComparisonChart.resize();
            if (app.sentimentDistributionChart) app.sentimentDistributionChart.resize();
            if (app.activityTrendChart) app.activityTrendChart.resize();
            if (app.topicDistributionChart) app.topicDistributionChart.resize();
        });
    </script>
</body>
</html>